package com.meepo.framework.design.tabs.sample

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.meepo.framework.design.tabs.MagicIndicator
import com.meepo.framework.design.tabs.buildins.commonnavigator.CommonNavigator
import com.meepo.framework.design.tabs.buildins.commonnavigator.abs.CommonNavigatorAdapter
import com.meepo.framework.design.tabs.buildins.commonnavigator.abs.IPagerIndicator
import com.meepo.framework.design.tabs.buildins.commonnavigator.abs.IPagerTitleView
import com.meepo.framework.design.tabs.buildins.commonnavigator.titles.CommonPagerTitleView
import com.meepo.sdk.utils.Func

object MagicIndicatorManager {

    fun bindWithFragment(
        context: Context,
        magicIndicator: MagicIndicator,
        tabs: List<TabItem>,
        fragmentContainerManager: FragmentContainerManager
    ) {

        val commonNavigator = CommonNavigator(context)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int = tabs.size

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {

                val tab = tabs[index]

                val commonPagerTitleView = CommonPagerTitleView(context)

                // load custom layout
                val customLayout: View = LayoutInflater.from(context).inflate(tab.itemLayoutId, null)
                val titleImg = customLayout.findViewById<View>(tab.iconId) as? ImageView
                val titleText = customLayout.findViewById<View>(tab.textId) as? TextView

                titleImg?.setImageResource(tab.unSelectedIcon)
                titleText?.text = tab.unSelectedText

                commonPagerTitleView.setContentView(customLayout)


                commonPagerTitleView.onPagerTitleChangeListener = object : CommonPagerTitleView.OnPagerTitleChangeListener {
                    override fun onSelected(index: Int, totalCount: Int) {
                        titleText?.setTextColor(tab.selectedTextColor)
                        titleText?.text = tab.selectedText
                        titleImg?.setImageResource(tab.selectedIcon)
                    }

                    override fun onDeselected(index: Int, totalCount: Int) {
                        titleText?.setTextColor(tab.unSelectedTextColor)
                        titleText?.text = com.meepo.sdk.utils.Func.orString(tab.unSelectedText, tab.selectedText)
                        titleImg?.setImageResource(tab.unSelectedIcon)
                    }

                    override fun onLeave(index: Int, totalCount: Int, leavePercent: Float, leftToRight: Boolean) {
                        titleImg?.scaleX = 1.1f + (0.9f - 1.1f) * leavePercent
                        titleImg?.scaleY = 1.1f + (0.9f - 1.1f) * leavePercent
                    }

                    override fun onEnter(index: Int, totalCount: Int, enterPercent: Float, leftToRight: Boolean) {
                        titleImg?.scaleX = 0.9f + (1.1f - 0.9f) * enterPercent
                        titleImg?.scaleY = 0.9f + (1.1f - 0.9f) * enterPercent
                    }
                }

                commonPagerTitleView.setOnClickListener {
                    fragmentContainerManager.switchPages(index)
                }

                return commonPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                return null
            }
        }

        magicIndicator.navigator = commonNavigator


        fragmentContainerManager.switchPages(0)
    }
}